﻿using System.Windows;
using System.Windows.Controls;
using Bookvar.Common;
using Bookvar.ObjectModel;
using Bookvar.UI.ViewModel.LiveImages;

namespace Bookvar.UI.DragDrop.Actions
{
    public static class ActionFactory
    {
        public static DropActionBase CreateDropAction( DragEventArgs args )
        {
            if ( args.Data.GetDataPresent( BookvarDataFormats.Topic.Name ) )
            {
                if ((args.Effects & DragDropEffects.Copy) == DragDropEffects.Copy)
                {
                    return new TopicCopyDropAction();
                }
                else
                {
                    return new TopicMoveDropAction();
                }
            }
            else if ( args.Data.GetDataPresent( DataFormats.FileDrop ) )
            {
                var fileNames = (string[]) args.Data.GetData( DataFormats.FileDrop );
                if ( fileNames != null && fileNames.Length > 0 )
                {
                    if ( FileUtility.IsImage( fileNames[ 0 ] ) )
                    {
                        return new ImageDropAction();
                    }
                    else
                    {
                        return new FileDropAction();
                    }
                }
            }
            else if ( args.Data.GetDataPresent( BookvarDataFormats.ImageInfo.Name ) )
            {
                var info = (ImageInfo) args.Data.GetData( BookvarDataFormats.ImageInfo.Name );
                if ( info != null )
                {
                    return new ImageDropAction();
                }
            }
            else if ( args.Data.GetDataPresent( BookvarDataFormats.LiveImageInfo.Name ) )
            {
                var info = (LiveImageInfo)args.Data.GetData(BookvarDataFormats.LiveImageInfo.Name);
                if ( info != null )
                {
                    return new ImageDropAction();
                }
            }

            return new TopicMoveDropAction();
        }

        public static DragActionBase CreateDragAction( UIElement draggedElement, DragDropEffects finalEffects )
        {
            if ( draggedElement is TopicContentPresenter )
            {
                if ( ( finalEffects & DragDropEffects.Move ) == DragDropEffects.Move )
                {
                    return new TopicMoveDragAction();
                }
//                else if ((finalEffects & DragDropEffects.Copy) == DragDropEffects.Copy)
//                {
//                    return new TopicCopyDragAction();
//                }
            }
            else if ( draggedElement is Image )
            {
                return new ImageDragAction();
            }
            return new TopicMoveDragAction();
        }
    }
}